// Draw.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Draw.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "DrawDoc.h"
#include "NewImageDlg.h"
#include "Splash.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDrawApp

BEGIN_MESSAGE_MAP(CDrawApp, CWinApp)
	//{{AFX_MSG_MAP(CDrawApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_EDIT_PASTE_IMAGE, OnEditPasteImage)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_IMAGE, OnUpdateEditPasteImage)
	ON_COMMAND(ID_EDIT_EMPTY, OnEditEmpty)
	ON_UPDATE_COMMAND_UI(ID_EDIT_EMPTY, OnUpdateEditEmpty)
	ON_COMMAND(ID_WINDOW_CLOSEALL, OnWindowCloseall)
	ON_UPDATE_COMMAND_UI(ID_WINDOW_CLOSEALL, OnUpdateWindowCloseall)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDrawApp construction

CDrawApp::CDrawApp()
{
	// set the default para of the programe
	m_bNewFromPaste = FALSE;
	m_nNewWidth = 600;		// set the default width of new image
	m_nNewHeight = 450;		// set the default height of new image
	m_bCAD = FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDrawApp object

CDrawApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDrawApp initialization

BOOL CDrawApp::InitInstance()
{
	// CG: The following block was added by the Splash Screen component.
\
	{
\
		CCommandLineInfo cmdInfo;
\
		ParseCommandLine(cmdInfo);
\

\
		CDrawSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
\
	}
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CMultiDocTemplate* pDocTemplate;
	pDocTemplate = new CMultiDocTemplate(
		IDR_DRAWTYPE,
		RUNTIME_CLASS(CDrawDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CDrawView));
	AddDocTemplate(pDocTemplate);

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Enable drag/drop open
	m_pMainWnd->DragAcceptFiles();

	// Enable DDE Execute open
	EnableShellOpen();
	RegisterShellFileTypes(TRUE);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
		cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	//
	// reSet the tile text :
	//
	AfxGetMainWnd()->SetWindowText(
		_T("Microsoft Thunder BmpShop 2006 (CS)")); 

	// The main window has been initialized, so show and update it.
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CDrawApp::OnAppAbout()
{
	// show the about dlg on the screen
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CDrawApp message handlers


void CDrawApp::OnFileNew()
{
	CNewImageDlg dlg;				// initilizing the NewImage dlg
	dlg.m_nWidth = m_nNewWidth;		// set the default para of the dlg
	dlg.m_nHeight = m_nNewHeight;	// set the default para of the dlg
	if (dlg.DoModal() == IDOK)		// show the New Image dlg
	{
		m_nNewWidth = dlg.m_nWidth;	// get the input from user
		m_nNewHeight = dlg.m_nHeight;
		m_bCAD = dlg.m_bCAD;
		CWinApp::OnFileNew();
	}
}

/////////////////////////////////////////////////////////////////////////////
// CDrawApp message handlers

CDrawApp* GetApp()
{
	// get the app object's pointer
	return (CDrawApp*) AfxGetApp();
}

CMainFrame* GetMainFrame()
{
	// get mainframe object's pointer
	return (CMainFrame*) AfxGetMainWnd();
}

CDrawView* GetActiveDrawView()
{
	return (CDrawView*) ((CMainFrame*) AfxGetMainWnd())->GetActiveView();
}

CDrawDoc* GetActiveDrawDoc()
{
	return (CDrawDoc*) (GetActiveDrawView()->GetDocument());
}

// Function : OnEditPasteImage
// paste the image in the clickboard as a new image
//
void CDrawApp::OnEditPasteImage() 
{
	POSITION posTemplate = GetFirstDocTemplatePosition();
	CDocTemplate* pDocTemplate = GetNextDocTemplate(posTemplate);
	m_bNewFromPaste = TRUE;			// indicate the new image is created from paste

	// create a new null document template.
	CDrawDoc* pDocument = (CDrawDoc*) pDocTemplate->OpenDocumentFile(NULL);
	
	// create the new image object in the doc class
	pDocument->GetCxImage()->PasteFromClipboard();

	// save the initial state of the image into the stack.
	pDocument->GetCxImage()->SaveState(_T("Origin"));
	pDocument->SetModifiedFlag(TRUE);

	// update all the views of this image doc
	pDocument->UpdateAllViews(NULL);
	m_bNewFromPaste = FALSE;		// finish creating.
}

void CDrawApp::OnUpdateEditPasteImage(CCmdUI* pCmdUI) 
{
	// update the choice state of Paste in the menu
	pCmdUI->Enable(IsClipboardFormatAvailable(CF_DIB));
}

void CDrawApp::OnEditEmpty() 
{
	// make the clipboard empty
	if (OpenClipboard(NULL))	// open the clipboard
	{
		if (EmptyClipboard())	// make the clipboard empty
		CloseClipboard();		// close the clipboard
	}
}

void CDrawApp::OnUpdateEditEmpty(CCmdUI* pCmdUI) 
{
	// update the choice state of EditEmpty in the menu
	pCmdUI->Enable(CountClipboardFormats() != 0);
}

BOOL CDrawApp::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following lines were added by the Splash Screen component.
	if (CDrawSplashWnd::PreTranslateAppMessage(pMsg))
		return TRUE;

	return CWinApp::PreTranslateMessage(pMsg);
}

// Function : OnWindowCloseall
// close all the windows open in this programe.
//
void CDrawApp::OnWindowCloseall() 
{
	POSITION posTemplate = GetFirstDocTemplatePosition();
	while (posTemplate != NULL)
	{	// get the open window one by one
		CDocTemplate* pTemplate = GetNextDocTemplate(posTemplate);
		ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));

		// get doctemplate's doc
		POSITION posDoc = pTemplate->GetFirstDocPosition();
		while (posDoc != NULL)
		{
			CDocument* pDoc = pTemplate->GetNextDoc(posDoc);

			// remind the user saving all unsaved image
			if (!pDoc->SaveModified())
				return;        // user aborted

			// close the document
			pDoc->OnCloseDocument();
		}
	}
}

void CDrawApp::OnUpdateWindowCloseall(CCmdUI* pCmdUI) 
{
	// update the choice state of CloseAll in the menu
	pCmdUI->Enable(NumberOfOpenDocuments());
}

// Function : NumberOfOpenDocuments
// Get the num of all opened documents
//
int CDrawApp::NumberOfOpenDocuments() const
{
	int nOpenDocuments = 0;	// initializing the counter
	POSITION posTemplate = GetFirstDocTemplatePosition();
	while (posTemplate != NULL) 
	{
		// recorde the open documents one by one :
		CDocTemplate* pTemplate = GetNextDocTemplate(posTemplate);
		ASSERT(pTemplate->IsKindOf(RUNTIME_CLASS(CDocTemplate)));
		POSITION posDoc = pTemplate->GetFirstDocPosition();
		while (posDoc != NULL) 
		{
			CDocument* pDoc = pTemplate->GetNextDoc(posDoc);
			++nOpenDocuments;	// update the counter
		}
	}

	return nOpenDocuments;	// return the num of the open docs
}

// Function : OnInitDialog
// initialize the about dlg
//
BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//
	// make the dialog part transparent :
	//

	/* The SetWindowLong function changes an attribute of the specified
	window. The function also sets a 32-bit (long) value at the specified
	offset into the extra window memory of a window. */
	SetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE,
		GetWindowLong(this->GetSafeHwnd(), GWL_EXSTYLE) ^ 0x80000);

	// load the User32.dll
	HINSTANCE hInst = LoadLibrary("User32.DLL");
	if ( hInst )
	{
		typedef BOOL (WINAPI* MYFUNC) (HWND, COLORREF, BYTE, DWORD);
		MYFUNC fun = NULL;
		// get the SetLayeredWindowAttributes function pointer :
		fun = (MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
		if (fun)
			fun(this->GetSafeHwnd(), 0, 128, 2);
		FreeLibrary(hInst);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
